package net.oschina.git.fsppp.model;

import net.oschina.git.fsppp.utils.P3Utils;

import java.io.Serializable;

/**
 * 磁盘块
 */
public class P3Block implements Serializable {
    /**
     * id: int 块号
     */
    private int id;
    /**
     * content: Byte[] 块内容
     */
    private Byte[] content;
    /**
     * isDir: boolean 改块是否为文件夹
     */
    private boolean isDir;
    /**
     * dir: P3Dir 文件目录（文件）
     */
    private P3Dir dir;
    /**
     * 磁盘
     */
    private P3Disk disk;

    public P3Block(boolean isDir, int id) {
        this.isDir = isDir;
        this.id = id;
        this.content = new Byte[P3Utils.MAX_BLOCK_SIZE];
    }

    public P3Block(int id, Byte[] content, boolean isDir, P3Dir dir) {
        this.id = id;
        this.content = content;
        this.isDir = isDir;
        this.dir = dir;
    }

    public P3Block initBlock() {
        content = new Byte[P3Utils.MAX_BLOCK_SIZE];
        content[0] = (byte) this.id;
        if (this.isDir) {
            content[1] = 1;
        } else content[1] = 0;

        for (int i=2;i<P3Utils.MAX_BLOCK_SIZE;i++) {
            content[i] = 0;
        }
        return this;
    }

    public byte[] tobyte(){
        byte[] dist = new byte[P3Utils.MAX_BLOCK_SIZE];
        for (int i = 0; i < P3Utils.MAX_BLOCK_SIZE; i++) {
            dist[i] = this.content[i];
        }
        return dist;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Byte[] getContent() {
        return content;
    }

    public void setContent(Byte[] content) {
        this.content = content;
    }

    public void setDir(boolean dir) {
        isDir = dir;
    }

    public boolean isDir() {
        return isDir;
    }

    public void setIsDir(boolean isDir) {
        this.isDir = isDir;
    }

    public P3Dir getDir() {
        return dir;
    }

    public void setDir(P3Dir dir) {
        this.dir = dir;
    }

    public P3Disk getDisk() {
        return disk;
    }

    public void setDisk(P3Disk disk) {
        this.disk = disk;
    }

    @Override
    public int hashCode() {
        int hash = 17;

        hash = hash * 31 + this.id;

        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj == null || !this.getClass().equals(obj.getClass())) {
            return false;
        }

        P3Block pb = (P3Block) obj;

        return (
                this.id == pb.getId()
        );
    }

    @Override
    public String toString() {
        return "P3Block{" +
                "id=" + id +
                "disk=" + disk.getName() +
                ", isDir=" + isDir +
                '}';
    }
}
